package com.yy.train.ticket.train.controller;

import java.util.ArrayList;
import java.util.List;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.train.ticket.into.page.TrainPage;
import com.yy.train.ticket.train.entity.TtStationEntity;
import com.yy.train.ticket.train.entity.TtTrainBoxEntity;
import com.yy.train.ticket.train.service.TtTrainBoxService;
import com.yy.train.ticket.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yy.train.ticket.train.entity.TtTrainEntity;
import com.yy.train.ticket.train.service.TtTrainService;




/**
 * 列车
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-09-25 10:38:44
 */
@RestController
@RequestMapping("tt/train")
public class TtTrainController {
    @Autowired
    private TtTrainService ttTrainService;
    @Autowired
    private TtTrainBoxService ttTrainBoxService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(){
        List<TtTrainEntity> list = ttTrainService.list(Wrappers.<TtTrainEntity>lambdaQuery());
        return R.ok(list);

    }

    /**
     * 列表
     */
    @PostMapping("/page")
    public R page(@RequestBody TrainPage trainPage){
        LambdaQueryWrapper<TtTrainEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(trainPage.getTrainNo()), TtTrainEntity::getTrainNo,trainPage.getTrainNo());

        Page<TtTrainEntity> pages = new Page<TtTrainEntity>(trainPage.getPageNum(), trainPage.getPageSize());
        IPage<TtTrainEntity> iPage = ttTrainService.page(pages, lambdaQueryWrapper);
        return R.ok(iPage);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{trainId}")
    public R info(@PathVariable("trainId") Long trainId){
		TtTrainEntity ttTrain = ttTrainService.getById(trainId);

        return R.ok(ttTrain);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody TtTrainEntity ttTrain){
        Integer box= ttTrain.getBox();
		ttTrainService.save(ttTrain);
        Long trainId=ttTrain.getTrainId();
        List<TtTrainBoxEntity> list=new ArrayList<>();
        TtTrainBoxEntity ttTrainBox=null;
        for (int i = 1; i <box+1 ; i++) {
            ttTrainBox=new TtTrainBoxEntity();
            ttTrainBox.setBoxNo(i);
            ttTrainBox.setTrainId(trainId);
            list.add(ttTrainBox);
        }
        ttTrainBoxService.saveBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody TtTrainEntity ttTrain){
		ttTrainService.updateById(ttTrain);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Long> trainIds){
		ttTrainService.removeByIds(trainIds);

        return R.ok();
    }

}
